// request.js 完整修改
import axios from 'axios';

const request = axios.create({
  baseURL: '/api', // 配合 vite 代理
  timeout: 5000
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // 排除接口：不添加 Token,防止干扰！！！
    if (config.url !== '/users/login' && config.url != '/users/register' && config.url !== '/users/sendEmailCode' && config.url !== '/chatStream') {
      const token = sessionStorage.getItem('token');
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
    }
    return config;
  },
  (error) => Promise.reject(error)
);

request.interceptors.response.use(
  result => {
    return result.data;
  },
  err => {
    return Promise.reject(err);
  }
)

export default request;